package com.hzit.provider.controller;

import com.hzit.api.entity.Dept;
import com.hzit.provider.service.DeptService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class DeptController {

    @Autowired
    private DeptService service;

    /**
     * @param dept
     * @return
     * @RequestBody：json格式参数转为对象
     */
    @RequestMapping(value = "/dept/add", method = RequestMethod.POST)
    public boolean add(@RequestBody Dept dept) {
        return service.add(dept);
    }

    @RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)
    @HystrixCommand(fallbackMethod = "nullDeptFallBack")
    public Dept get(@PathVariable("id") Long id) {
        Dept dept = service.get(id);
        if (dept == null) {
            throw new RuntimeException("数据未找到...");
        }
        return dept;
    }

    /**
     * 返回：预定义的错误内容
     *
     * @param id
     * @return
     */
    public Dept nullDeptFallBack(@PathVariable("id") Long id) {
        System.out.println("deptno...出现故障");
        return new Dept().setDeptno(500).setDname("没有找到部门ID:" + id + "相关的信息").setLoc("no loc");
    }


    @RequestMapping(value = "/dept/list", method = RequestMethod.GET)
    public List<Dept> list() {
        return service.list();
    }
}
